package com.example.chapter05;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import util.ViewUtil;

public class EditHideActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_hide);
        EditText et_phone =findViewById(R.id.et_phone);
        EditText et_password =findViewById(R.id.et_password);

        et_password.addTextChangedListener(new HideTextWatcher(et_password,6));
        et_phone.addTextChangedListener(new HideTextWatcher(et_phone,11));

    }
    //定义一个编辑框监听器
    private class HideTextWatcher implements TextWatcher {
        //声明一个编辑框对象
        private EditText mView;
        //声明一个最大长度变量
        private int mMaxLength;
        public HideTextWatcher(EditText v, int maxLength) {
            this.mView = v;
            this.mMaxLength = maxLength;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

        }
        //在编辑框的输入文本变化后触发
        @Override
        public void afterTextChanged(Editable s) {
            //获得已输入的文本字符串
            String str = s.toString();
            //输入文本达到11位（如手机号码），或者到达6位时关闭输入法
            if (str.length()==mMaxLength){
                //隐藏输入法的键盘
                ViewUtil.hideOneInputMethod(EditHideActivity.this,mView);
            }
        }
    }
}